/**
 * �ӱ�ʡͨ�Ž������޹�˾��Ϣ���ֹ�˾ ��Ȩ����
 *
 * �ļ�:com.hebccc.util.Md5Util.java
 *
 * ����ʱ��:2012-12-25
 *
 * ������:���ڽ�
 */
package com.sxtyxt.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * md5工具类
 */
public class Md5Util {

	/**
	 * 获取md5值
	 * @param str
	 * @return
	 */
	public final static String getMd5(String str) {
		
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		messageDigest.update(str.getBytes());
		byte[] md = messageDigest.digest();
		return byteArrToHexStr(md);
	}

	/**
	 * 自己数组转16进制字符串
	 * @param source
	 * @return
	 */
	public static String byteArrToHexStr(byte[] source) {
		int length = source.length;
		StringBuffer stringBuffer = new StringBuffer(length * 2);
		for (int i = 0; i < length; i++) {
			int intTmp = source[i];
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			if (intTmp < 16) {
				stringBuffer.append("0");
			}
			stringBuffer.append(Integer.toString(intTmp, 16));
		}
		return stringBuffer.toString().toUpperCase();
	}


}
